package com.jiaz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiaz.algorithm.UserAlgorithm;
import com.jiaz.dto.UserBaseDTO;
import com.jiaz.dto.UserSimilaryDTO;
import com.jiaz.entity.User;
import com.jiaz.mapper.UserMapper;
import com.jiaz.service.UserService;
import com.jiaz.untils.ResponseBean;
import com.jiaz.untils.constant.VarConstant;
import com.jiaz.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2022/4/24 16:17
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAlgorithm userAlgorithm;


    @Override
    public ResponseBean getUserList(QueryVo queryVo) {
        if (ObjectUtils.isEmpty(queryVo)) {
            return ResponseBean.error(4008, "参数错误(空值)");
        }
        if (queryVo.getPageNum() == null) {
            queryVo.setPageNum(VarConstant.PAGE_NUM);
        }
        if (queryVo.getPageSize() == null) {
            queryVo.setPageSize(VarConstant.PAGE_SIZE);
        }

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","frequency");
        String key = queryVo.getKey();
        if(StringUtils.hasLength(key)){
            queryWrapper.like("id",key);
        }
        Page<User> page = new Page<>(queryVo.getPageNum(), queryVo.getPageSize());
        Page<User> userPage = userMapper.selectPage(page, queryWrapper);
        return ResponseBean.success(userPage);
    }

    @Override
    public ResponseBean getUserById(int userId) {
        User user = userMapper.selectById(userId);
        if ( user == null) {
            return ResponseBean.error(40007, "无此ID号用户");
        }
        return ResponseBean.success(user);
    }

    @Override
    public ResponseBean getTotalUser() {
        return ResponseBean.success(userMapper.selectCount(null));
    }

    /**
     * 获取用户前十 好友和喜欢的主题
     *
     * @param userId
     * @return
     */
    @Override
    public ResponseBean getUserBase(int userId) {
        DecimalFormat decimalFormat = new DecimalFormat("#.#####");
        HashMap<String, List<String>> resMap = new HashMap<>();
        List<UserBaseDTO> userCategorys = userMapper.getUserCategory(userId);
        List<String> collect1 = userCategorys.stream().map(r -> r.getKey1() + ":" + r.getVal2()).collect(Collectors.toList());
        resMap.put("userCategory", collect1);
        List<UserSimilaryDTO> userFriendByTradition = userAlgorithm.getUserFriendByTradition(userId);
        List<String> collect2 = userFriendByTradition.stream().map(r -> r.getUserId() + ":" + decimalFormat.format(r.getScore())).collect(Collectors.toList());
        resMap.put("userFriends", collect2);
        return ResponseBean.success(resMap);
    }
}
